package com.bdmc.report.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.report.domain.bo.ReportCarTimeBo;
import com.bdmc.report.domain.vo.ReportCarTimeVo;
import com.bdmc.report.domain.ReportCarTime;
import com.bdmc.report.mapper.ReportCarTimeMapper;
import com.bdmc.report.service.IReportCarTimeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 发动机运行时长Service业务层处理
 *
 * @author bdmc
 * @date 2022-08-09
 */
@Service
public class ReportCarTimeServiceImpl extends ServicePlusImpl<ReportCarTimeMapper, ReportCarTime, ReportCarTimeVo> implements IReportCarTimeService {

    @Override
    public ReportCarTimeVo queryById(Long timeId){
        return getVoById(timeId);
    }

    @Override
    public TableDataInfo<ReportCarTimeVo> queryPageList(ReportCarTimeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportCarTime> lqw = buildQueryWrapper(bo);
        Page<ReportCarTimeVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<ReportCarTimeVo> mySelectPage(ReportCarTimeBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<ReportCarTimeVo> queryList(ReportCarTimeBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<ReportCarTime> buildQueryWrapper(ReportCarTimeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportCarTime> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPersonId() != null, ReportCarTime::getPersonId, bo.getPersonId());
        lqw.eq(bo.getCarId() != null, ReportCarTime::getCarId, bo.getCarId());
        lqw.eq(bo.getFenceId() != null, ReportCarTime::getFenceId, bo.getFenceId());
        lqw.eq(bo.getDataTime() != null, ReportCarTime::getDataTime, bo.getDataTime());
        lqw.eq(bo.getStartTime() != null, ReportCarTime::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, ReportCarTime::getEndTime, bo.getEndTime());
        lqw.eq(bo.getRunningTime() != null, ReportCarTime::getRunningTime, bo.getRunningTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(ReportCarTimeBo bo) {
        ReportCarTime add = BeanUtil.toBean(bo, ReportCarTime.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setTimeId(add.getTimeId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(ReportCarTimeBo bo) {
        ReportCarTime update = BeanUtil.toBean(bo, ReportCarTime.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ReportCarTime entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
